package com.ecmcloud.flow.node.impl;

import cn.hutool.core.collection.CollUtil;

import com.ecmcloud.common.constants.ProcessInstanceConstant;
import com.ecmcloud.common.dto.Result;
import com.ecmcloud.common.dto.flow.Node;
import com.ecmcloud.common.dto.flow.NodeUser;
import com.ecmcloud.common.dto.third.DeptDto;
import com.ecmcloud.flow.node.AssignUserStrategy;
import com.ecmcloud.flow.utils.BizHttpUtil;
import com.google.common.collect.Lists;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * 指定主管
 */
@Component
public class AssignUserLeaderStrategyImpl implements InitializingBean, AssignUserStrategy {
	
	@Override
	public List<String> handle(Node node, NodeUser rootUser, Map<String, Object> variables) {
		List<String> userIdList = Lists.newArrayList();
		// 指定主管审批
		// 第几级主管审批
		Integer level = node.getDeptLeaderLevel();
		// 去获取主管
		Result<List<com.ecmcloud.common.dto.third.DeptDto>> r = BizHttpUtil.queryParentDepListByUserId((rootUser.getId()));
		List<com.ecmcloud.common.dto.third.DeptDto> deptDtoList = r.getData();
		if (CollUtil.isNotEmpty(deptDtoList)) {
			if (deptDtoList.size() >= level) {
				DeptDto deptDto = deptDtoList.get(level - 1);
				List<String> leaderUserIdList = deptDto.getLeaderUserIdList();
				if (CollUtil.isNotEmpty(leaderUserIdList)) {
					for (String s : leaderUserIdList) {
						if (userIdList.contains(s)) {
							continue;
						}
						userIdList.add(s);
					}
				}
			}
		}
		return userIdList;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		afterPropertiesSet(ProcessInstanceConstant.AssignedTypeClass.LEADER);
	}
}
